'use strict';

import * as moment from 'moment'
import utils from 'src/serverCore/utils';
import mongodbHandle from 'src/handle/mongodbHandle';
import CountersDao from './counterDao';

class PlayerDao {
    handle: any
    constructor(db) {
        this.handle = db.collection("player")
        this.handle.createIndex("uID", 1)
        this.handle.createIndex("id", 1)
        this.handle.createIndex("name", 1)
        this.handle.createIndex("currentServerID", 1)
        this.handle.createIndex("loginTime", 1)
        this.handle.createIndex({ "currentServerID": 1, "cheat": 1, "phone": 1, "mapProgressMax": -1 })
        this.handle.createIndex({ "currentServerID": 1, "cheat": 1, "phone": 1, "arenaScore": -1 })
        this.handle.createIndex({ "currentServerID": 1, "cheat": 1, "phone": 1, "totalPower": -1 })
        this.handle.createIndex({ "currentServerID": 1, "cheat": 1, "phone": 1, "abyssProgress": -1 })
        this.handle.createIndex({ "currentServerID": 1, "cheat": 1, "phone": 1, "loginTime": 1 })
        this.handle.createIndex({ "currentServerID": 1, "cheat": 1, "mapProgressMax": -1 })
        this.handle.createIndex({ "currentServerID": 1, "cheat": 1, "arenaScore": -1 })
        this.handle.createIndex({ "currentServerID": 1, "cheat": 1, "totalPower": -1 })
        this.handle.createIndex({ "currentServerID": 1, "cheat": 1, "abyssProgress": -1 })
        this.handle.createIndex({ "currentServerID": 1, "cheat": 1, "loginTime": 1 })
    }

    newAndSave = async (uID, originServerID, playerData, phone): Promise<any> => {
        let date = new Date().getTime()
        let id = await mongodbHandle.countersDao.getNextSequenceValue('player')
        let idLable = phone == 'android' ? 'a' : 'i'
        return await this.handle.insertOne({
            id: "da" + originServerID + idLable + utils.randomNumberString(2) + id,
            uID: uID,
            name: null,
            playerData: playerData,
            totalPower: 0, //总战斗力
            totalPowerTime: 0, //总战斗力刷新时间
            equipmentPower: {},
            worldBossDamage: 0, //世界boss伤害
            mapProgressMax: 0, //进度
            arenaTimes: 0, //竞技次数
            arenaTime: moment().startOf('day').valueOf(), //自动竞技时间
            arenaAutoTimes: 0, //自动竞技次数
            arenaAutoTime: moment().startOf('day').valueOf(), //自动竞技时间
            areaCsTimes: 0, //跨服竞技次数
            areaCsTime: moment().startOf('day').valueOf(), //跨服自动竞技时间
            arenaPoint: 0,
            csArenaPoint: 0,
            abyssProgress: 0,
            abyssDayTime: 0,
            registerTime: date,
            loginTime: date,
            saveTime: date,
            cheat: false,
            originServerID: originServerID || 0, //原始服务器id
            currentServerID: originServerID || 0, //当前服务器id, 可能是原来的,也可能是合并的
            chargeMoney: 0, //充值金额
            modify: 'init',
            modifyTime: 0,
            modifyTimes: [],
            cheatTimes: 0,
            phone: phone == 'android' ? 'android' : 'ios',
            arenaScore: 100 * 100000000,
            vipTime: 0, //vip到期时间
            vipExp: 0, //vip经验
            deviceID: "", //设备id
            clientLocalTime: date,
            overTime: date,
            bakTime: date, //备份时间
            saveAmount: 0, //客户端保持序号
        });
    }

    findOne = async (query): Promise<any> => {
        return await this.handle.findOne(query)
    }

    find = async (query): Promise<any[]> => {
        return await this.handle.find(query).toArray()
    }

    findByPage = async (query, start: number, size: number): Promise<any[]> => {
        return await this.handle.find(query).skip(start).limit(size).toArray()
    }

    findByPageAndSort = async (query, sort, start, size): Promise<any[]> => {
        return await this.handle.find(query).sort(sort).skip(start).limit(size).toArray()
    }

    findAndSort = async (query, sort): Promise<any[]> => {
        return await this.handle.find(query).sort(sort).toArray()
    }
    //opts  { multi: true}
    updateMany = async (query, data): Promise<any> => {
        return await this.handle.updateMany(query, data);
    }

    updateOne = async (query, data): Promise<any> => {
        return await this.handle.updateOne(query, data);
    }

    count = async (query): Promise<number> => {
        return await this.handle.countDocuments(query)
    }

    remove = async (query): Promise<any> => {
        return await this.handle.remove(query, {
            multi: true
        });
    }
}

export default PlayerDao